โฝ๏ธ ๋ฐฐํฌ
์ด์ ๊ธ์์ ๋ค๋ฃจ์๋ฏ, GCP ํ๊ฒฝ์์ lighthouse ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์์๋ค. ๋ฐ๋ผ์ ์คํ๋ง ๋ถํธ ๋ฐฐํฌ ๋ํ GCP ํ๊ฒฝ์ ์ํํด์ผ ํ๋ค.
์ด์ ์ ๋ช ๋ฒ ์๋ฒ ๋ฐฐํฌ ์์๋, AWS๋ฅผ ์ฌ์ฉํ์ด์ ๊ฝค๋ ๋ฅ์ํ๊ฒ ๋ฐฐํฌํ ์ ์์์ง๋ง ์ด๋ฒ์ ์ฒ์์ผ๋ก GCP๋ฅผ ์ฌ์ฉํด์ ์์ฒญ ๋ง์ด ํค๋งธ๋ค ...
์๋์ ์ฐธ๊ณ ์ฌ์ดํธ๋ค์ ํตํด ํด๊ฒฐํ ์ ์์๊ณ , ์ ์ฒด์ ์ธ ํ์ ๊ฑฐ์ ์ ์ฌํ๋ค.
- ๋ค๋ง, ssh์ ๋ธ๋ผ์ฐ์ ๋ฅผ ํตํด ์ฐ๊ฒฐํ ์ ์์๊ณ
- ํ๋ก์ ํธ ๋น๋ ํ ๋ง๋ค์ด์ง jar ํ์ผ์ ๋ธ๋ผ์ฐ์ ssh์์ ๋ค์ด๋ฐ์ ๊ตฌ๋ํ ์ ์๋ค๋ ์ ์ด ๋ฌ๋๋ค.
์ด์ AWS๋ฅผ ์ฌ์ฉํ์ ๋๋ ssh์์ ํ๋ก์ ํธ ๊น ์ฃผ์๋ฅผ clone ๋ฐ์ ์ง์ ๋น๋ํ๊ณ ๋์์์ผฐ์๋ค.
์ด์ฐ์ ์ฐ ์ธ์คํด์ค๋ฅผ ๋ง๋ค๊ณ SQL๋ ์์ฑํ์ฌ, ์์ง๊น์ง๋ ์๋ฌด๊ฒ๋ ๋ง๋ค์ด์ง์ง ์์ ํ๋ก์ ํธ๋ฅผ ๋น๋ํ์ฌ jar ํ์ผ์ ๋์์์ผฐ๊ณ , ์ ์์ ์ผ๋ก ์๋ํ๋ ๊ฒ์ ํ์ธํ๋ค.
๋ค๋ง, ์์ง mysql์ด ์ ๋๋ก ์ฐ๊ฒฐ๋์๋์ง๋ ์ ๋ชจ๋ฅด๊ฒ ๋ค ... ์ผ๋จ IntelliJ์์ Database ์ฐ๊ฒฐ์ ๋์์ผ๋, ์ถํ DB ์ ๊ทผ ๋ก์ง์ ๊ตฌํํ ํ ๋ค์ ํ์ธํด์ผ ํ ๊ฒ ๊ฐ๋ค.
โฝ๏ธ Spring REST Docs ์ ์ฉ
์์ง ์ค๊ณ๊ณผ์ ์ด ๋ ๊ตฌ์ฒดํ๋์ด์ผ ํ๊ธฐ์, ์ฐ์ ์ฌ์ฉํ API๋ฅผ ์ ์ํ๊ณ , ๊ฐ๋จํ ๋ฐํ์ ํ๋๋ก ๊ตฌํํ๋ค.
์ฌ์ค ์ด๋ฅผ ํ ์ด์ ๋ Spring REST docs๋ฅผ ์ ์ฉํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ด๋ค. ๊ฐ๋จํ๊ฒ ์ฝ๋๋ฅผ ๊ตฌํํด๋๊ณ , ์ด์ ๋ํ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํด Spring REST docs๋ฅผ ์ ์ฉํด ํ๋ก ํธ์๋๋จ์์ ๊ฐ๋ฐ ์ ์ฐธ๊ณ ํ ๋ฌธ์๋ฅผ ๋น ๋ฅด๊ฒ ๋ง๋ค ๊ฒ์ด๋ค.
์ด์ ์๋ Swagger ui๋ฅผ ์ฌ์ฉํ์๋๋ฐ, ์ฌ์ค Spring REST docs๋ผ๋ ๊ฒ์ด ์๋์ง ๋ชฐ๋๋ค๋ ์ด์ ๊ฐ ์ปธ๋ค. Spring REST docs๊ฐ ์ ์ฉ์ด ์ด๋ ต๋ค๋ ๋จ์ ์ด ์๊ธด ํ์ง๋ง, ์ต๊ทผ ํ ์คํธ ์ฝ๋๋ฅผ ์ ์์ฑํ๋ ๊ฐ๋ฐ์๊ฐ ๋๊ณ ์ ํ๋ ์๊ฐ์ ๊ฐ์ง๋ฉฐ, ์์ผ๋ก์ ํ๋ก์ ํธ์์๋ ๋ฌด์กฐ๊ฑด ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ์! ๋ผ๋ ๊ฒฐ์ฌ์ ํ๊ธฐ์ ํ ์คํธ ์ฝ๋ ์์ฑ์ ๊ฐ์ ํํ๋ Spring REST docs๋ฅผ ์ฌ์ฉํด๋ณด๊ณ ์ ํ๋ค.
์ด์ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ๋ ค๋๋ฐ, ์ฒ์ ๋ณด๋ Spring MockMvc๋ฅผ ์ฌ์ฉํด ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํด์ผ ํ๋ค๊ณ ํ๋ค. ์ฐธ๊ณ ๋ฅผ ํ์ธํ๋, "Spring REST Docs๋ Spring MVC์ ํ ์คํธ ํ๋ ์ ์ํฌ ๋๋ Rest Assured 3๋ก ์์ฑ๋ ํ ์คํธ ์ฝ๋์์ ์์ฑ๋ Snippet์ ์ฌ์ฉํ๋ค." ๋ผ๋ ์ด์ ๊ฐ ์์๋ค.
๊ทธ๋ ๋ค๋ฉด, Rest Assured 3๋ฅผ ์ฌ์ฉํ๋ ํ ์คํธ ์ฝ๋ ์์ฑ์ด ์๋๋ผ ์ Spring MockMvc๋ฅผ ์ฌ์ฉํ ๊น?
MockMvc๋ ๋ธ๋ผ์ฐ์ ์์์ ์์ฒญ๊ณผ ์๋ต์ ์๋ฏธํ๋ ๊ฐ์ฒด๋ก Controller ํ ์คํธ๋ฅผ ์ฉ์ดํ๊ฒ ํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก Controller ๋ ์ด์ด๋ง ํ ์คํธํ๊ธฐ์ ์๋๊ฐ ๋น ๋ฅด์ง๋ง, Rest Assured์ ๊ฒฝ์ฐ ์ ์ฒด ์ปจํ ์คํธ๋ฅผ ๋ก๋ํ๊ณ ๋น์ ์ฃผ์ ํ๊ธฐ ๋๋ฌธ์ ์๋๊ฐ ๋๋ฆฌ๋ค.
์ด๋ฌํ ์ด์ ๋ก ์ธํด Spring MockMvc๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ ๊ฒ ๊ฐ๋ค!
๐ MockMvc
์น ์ดํ๋ฆฌ์ผ์ด์ ์ ์ ํ๋ฆฌ์ผ์ด์ ์๋ฒ์ ๋ฐฐํฌํ์ง ์๊ณ ํ ์คํธ์ฉ MVC ํ๊ฒฝ์ ๋ง๋ค์ด ์์ฒญ ๋ฐ ์ ์ก, ์๋ต๊ธฐ๋ฅ์ ์ ๊ณตํด์ฃผ๋ ์ ํธ๋ฆฌํฐ ํด๋์ค๋ก, ์ปจํธ๋กค๋ฌ๋ฅผ ํ ์คํธํ๊ณ ์ถ์ ๋ ์ค์ ์๋ฒ์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋์์ํค๋ ๊ฒ์ด ์๋๋ผ, ๊ฐ์ง ์๋ฎฌ๋ ์ด์ ์ ํตํด MVC๊ฐ ๋๋๋ก ๋์์ฃผ๋ ํด๋์ค์ด๋ค.
package com.example.QCA.QualityControlAutomation.home;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
@AutoConfigureMockMvc
// Spring REST docs ์๋ ์ค์ ์ ์ํจ
@AutoConfigureRestDocs
class HomeControllerTest {
// MockMvc ๊ฐ์ฒด ์์ฑ
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@DisplayName("Top 5 ๋ฆฌ์คํธ ๋ฐํ ํ
์คํธ")
@Test
void listTest() throws Exception {
mockMvc.perform(
// ์์ฒญ์ ๋ํ ์ ๋ณด ์
๋ ฅ
get("/api/list")
.contentType(MediaType.APPLICATION_JSON)
)
// ์๋ต์ด ok์ธ์ง ํ
์คํธ
.andExpect(status().isOk())
// ์๋ต์ด JSON์ธ์ง ํ
์คํธ
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andDo(
// ์ด๋ ๋๋ ํ ๋ฆฌ์ ์ ์ฅํ ๊ฒ์ธ์ง ๋ช
์
document(
"list-get",
responseFields(
fieldWithPath("status").description("์ํ ์ฝ๋"),
fieldWithPath("message").description("๋ฐํ ๋ฉ์ธ์ง"),
fieldWithPath("data").type(JsonFieldType.ARRAY).description("๊ฒฐ๊ณผ"),
fieldWithPath("data[].label").type(JsonFieldType.STRING).description("์น์ฌ์ดํธ ๋ช
"),
fieldWithPath("data[].homepage").type(JsonFieldType.STRING).description("์น์ฌ์ดํธ ์ฃผ์"),
fieldWithPath("data[].totalScore").type(JsonFieldType.NUMBER).description("์น์ฌ์ดํธ์ ๊ฒฐ๊ณผ ์ดํฉ")
)
)
)
;
}
}
MockMvc๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.MockMvc๊ฐ์ฒด์ ์์ฒญ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๋ฌํ๋ค.-
์๋ต๊ฐ์
.andExpect()๋ฅผ ํตํด ๊ฒ์ฆํ๋ค..andExpect()๋ ์ฌ๋ฌ ๊ฐ๊ฐ ์ฌ ์ ์๊ณ , ๋ชจ๋ ํต๊ณผํ๋ค๋ฉด ํ ์คํธ ํต๊ณผ
.andDo()๋ฅผ ์ด์ฉํด ํ ์คํธ ์ฑ๊ณต ํ ์ํํ ํ๋์ ๋ช ์ํ๋ค.responseFields์ ๋ฌธ์์ ์ ํ ๋ฐํ ํํ์ ์ค๋ช ์ ๋ช ์ํ๋ค.
์ด๋ ์ด๋
ธํ
์ด์
์ค @WebMvcTest ๋ฅผ ํ์ธํ ์ ์๋๋ฐ, ์ฒ์์๋ @SpringBootTest ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์๋ค. ํ์ง๋ง ์ฌ๊ธฐ์๋ Controller์ ๋ํ ํ
์คํธ๋ง ์ํํ ์์ ์ด๋ฏ๋ก, ๋น์ฐํ @WebMvcTest ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก ์์ ํ๋ค.
๊ฐ๋จํ ๊ตฌํ ์ฝ๋์ฌ์ ํ ์คํธ๋ ๊ฐ๋จํ๊ฒ ์ฑ๊ณตํ๋ค. ์ด์ REST docs๋ฅผ ์ ์ฉํ๋ฌ ๊ฐ๋ณด์.
๐ Spring REST Docs ์ ์ฉ
์๋ ์ฐธ๊ณ ๋ฅผ ๋ณด๊ณ , build.gradle ์ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ค.
plugins {
id 'org.springframework.boot' version '2.7.2'
id 'io.spring.dependency-management' version '1.0.12.RELEASE'
id "org.asciidoctor.jvm.convert" version "3.3.2"
id 'java'
}
group = 'com.example.QCA'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
ext {
set('snippetsDir', file("build/generated-snippets"))
}
configurations {
asciidoctorExtensions
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// Spring REST docs
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
asciidoctorExtensions 'org.springframework.restdocs:spring-restdocs-asciidoctor'
}
test {
outputs.dir snippetsDir
useJUnitPlatform()
}
asciidoctor { // asciidoctor ์์
๊ตฌ์ฑ
dependsOn test // test ์์
์ดํ์ ์๋ํ๋๋ก ํ๋ ์ค์
configurations 'asciidoctorExtensions' // ์์์ ์์ฑํ configuration ์ ์ฉ
inputs.dir snippetsDir // snippetsDir ๋ฅผ ์
๋ ฅ์ผ๋ก ๊ตฌ์ฑ
// source๊ฐ ์์ผ๋ฉด .adocํ์ผ์ ์ ๋ถ html๋ก ๋ง๋ค์ด๋ฒ๋ฆผ
// source ์ง์ ์ ํน์ adoc๋ง HTML๋ก ๋ง๋ ๋ค.
sources{
include("**/index.adoc","**/common/*.adoc")
}
// ํน์ .adoc์ ๋ค๋ฅธ adoc ํ์ผ์ ๊ฐ์ ธ์์(include) ์ฌ์ฉํ๊ณ ์ถ์ ๊ฒฝ์ฐ ๊ฒฝ๋ก๋ฅผ baseDir๋ก ๋ง์ถฐ์ฃผ๋ ์ค์
// ๊ฐ๋ณ adoc์ผ๋ก ์ด์ํ๋ค๋ฉด ํ์ ์๋ ์ต์
baseDirFollowsSourceFile()
}
// static/docs ํด๋ ๋น์ฐ๊ธฐ
asciidoctor.doFirst {
delete file('src/main/resources/static/docs')
}
// asccidoctor ์์
์ดํ ์์ฑ๋ HTML ํ์ผ์ static/docs ๋ก copy
task copyDocument(type: Copy) {
dependsOn asciidoctor
from file("build/docs/asciidoc")
into file("src/main/resources/static/docs")
}
// build ์ ์์กด์์
๋ช
์
build {
dependsOn copyDocument
}
gradle์ ๋น๋ํ ํ, ํ๋ก์ ํธ๋ฅผ ๋น๋ํ๋ฉด build/generated-snippets ํด๋์ ์๋์ ๊ฐ์ด snippets ๊ฐ ์์ฑ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด์ ๋ง๋ค์ด์ง ์ฝ๋ ์กฐ๊ฐ์ ์ด์ฉํด ๋ฌธ์๋ฅผ ๋ง๋ค ์ฐจ๋ก์ด๋ค. ์ด๋ฅผ ์ํด main/resources/static/docs ๋๋ ํ ๋ฆฌ๋ฅผ ๋ง๋ ๋ค.
gradle์ค์ ์ ์ํด ์ด ๋๋ ํ ๋ฆฌ๋กhtmlํ์ผ์ด ๋ณต์ฌ๋์ด ์ฎ๊ฒจ์ง๋ค.
๊ทธ๋ฆฌ๊ณ src/docs/asciidoc ๋๋ ํ ๋ฆฌ๋ฅผ ๋ง๋ค๊ณ ์์ index.adoc ํ์ผ์ ์์ฑํ๋ค.
= API ๋ฌธ์
:doctype: book
:icons: font
// ๋ฌธ์์ ํ๊ธฐ๋๋ ์ฝ๋๋ค์ ํ์ด๋ผ์ดํ
:source-highlighter: highlightjs
// toc (Table Of Contents) ์์น ์ง์
:toc: left
:toclevels: 2
:sectlinks:
[[List-API]]
== List API
ํน์ ์ ๋ณด ๋ฐํ
// ์ด๋ค ์ค๋ํซ๋ค์ ์ฌ์ฉํ ๊ฒ์ธ์ง ๋ช
์
operation::list-get[snippets='http-request,http-response,response-fields']
- Asciidocs๋ฌธ๋ฒ์์ ๋ ์์ธํ ์ฌ์ฉ๋ฒ์ ํ์ธํ ์ ์๋ค.
- ์ด์ ๋น๋๋ฅผ ํ๋ฉด, ์๊น ์ง์ ํ ๋๋ ํ ๋ฆฌ์
index.html์ด ๋ง๋ค์ด์ง๊ณ , ์๋ฒ๋ฅผ ๋์ํด../docs/index.html์์ ํ์ธํ ์ ์๋ค.
์ถ๊ฐ์ ์ธ ์ต์ ํ์ ์ปค์คํ ์ ์ถํ ์ํํ ์์ ์ด๋ค.
โ๏ธ ๊ตฌํ ์ฝ๋๋ Github์์ ํ์ธํ ์ ์์ต๋๋ค ....
โฝ๏ธ ์ฐธ๊ณ
GCP ์ฌ์ฉ
- [GCP]GCP ๊ธฐ์ด_Cloud SQL ๋ง๋ค๊ธฐ
- Spring Boot์ Google Cloud SQL์ฐ๊ฒฐ ๋ฐ GCP(Google Cloud Platform) ์๋ฒ ๋ฐฐํฌ#2
Spring REST Docs, MockMvc